import NotificationMgr from "../../Managers/NotificationMgr";
import LanguageMngTs from "../../Managers/LanguageMngTs";

const { ccclass, property } = cc._decorator;

@ccclass
export default class LobbyScrollMsgTs extends cc.Component {

    @property(cc.Node)
    maskNode: cc.Node = null;

    @property(cc.Label)
    label: cc.Label = null;

    @property(cc.Node)
    ScrollMsgList: cc.Node = null;

    @property(cc.Sprite)
    lobbyScrollMsgBg: cc.Sprite = null;

    @property(cc.Prefab)
    listItem: cc.Prefab = null;


    /**
     * 滚动内容
     */
    // public contentArr: Array<string> = new Array<string>()

    public startPos: cc.Vec2 = null
    // public scrollItemData: Array<string> = [];

    public maxCount: number = 0;


    public onLoad() {


        this.lobbyScrollMsgBg.node.active = false;
        this.ScrollMsgList.active = false;

        this.startPos = cc.v2(this.maskNode.width / 2, 0)
        this.label.node.position = this.startPos

        this.maskNode.on(cc.Node.EventType.TOUCH_START, () => {
            this.ScrollMsgList.active = true;
            this.lobbyScrollMsgBg.node.active = true;
        })

        this.lobbyScrollMsgBg.node.on(cc.Node.EventType.TOUCH_START, () => {
            this.ScrollMsgList.active = false
            this.lobbyScrollMsgBg.node.active = false;
        })
    }

    public setInfo(data1: any, data2?) {

        if (data1 != null) {
            let isshow: boolean = NotificationMgr.Isshow;
            for (const v of data1) {
                let str1: string = "";
                let str2: string = "";
                let str3: string = "";
                if (v.type == 1) {   //"维护通知:
                    str1 = LanguageMngTs.getSentence(354) + " ";
                    let times = v.args.split(`,`);
                    str2 = this.timestampToTime(times[0]) + " - " + this.timestampToTime(times[1]);
                    str3 = str1 + str2;
                    let data = { content: str3, notifiId: v.id }
                    this.startScroll(data);
                    if (Number(times[2]) >= 0) {
                        if (isshow == true) {
                            NotificationMgr.getInstance().fixedStartScroll(Number(times[2]));
                            NotificationMgr.Isshow = false;
                        }
                        // this.fixedStartScroll(Number(times[2]), str3)

                    }
                } else if (v.type == 2) {   // 自定义通知
                    let data = { content: v.args, notifiId: v.id }
                    this.startScroll(data);
                }
            }
        }

        // 实时通知消息  type == 1 充值   type == 2 赢钱   type == 3 提现   4：排行榜变更
        if (data2 != null) {
            let args = data2.args.split(`,`);
            if (data2.type == 1) {
                let str1: string = args[0] + " " + LanguageMngTs.getSentence(355) + " " + cc.gg.gameDataUtil.getStringOfRupee(Number(args[1]));
                let data = { content: str1, notifiId: null }
                this.startScroll(data);
            } else if (data2.type == 2) {
                let str1: string = args[0] + " " + LanguageMngTs.getSentence(356) + " " + cc.gg.gameDataUtil.getStringOfRupee(Number(args[1]));
                let data = { content: str1, notifiId: null }
                this.startScroll(data);
            } else if (data2.type == 3) {
                // cc.gg.gameDataUtil.getStringOfRupee
                let str1: string = args[0] + " " + LanguageMngTs.getSentence(357) + " " + cc.gg.gameDataUtil.getStringOfRupee(Number(args[1]));
                let data = { content: str1, notifiId: null }
                this.startScroll(data);
            } else if (data2.type == 4) {
                // Zhang San surpasses Li Si  got daily server rank 12
                let str1: string = args[1] + " " + LanguageMngTs.getSentence(358) + " " + args[0] + " " + LanguageMngTs.getSentence(383) + args[2];
                let data = { content: str1, notifiId: null }
                this.startScroll(data);
            }
        }
    }

    // public getfixedMissage(textStr: string, times: number) {
    //     this.startScroll(textStr, 1);
    //     this.fixedStartScroll(times, textStr);
    // }

    // public fixedStartScroll(seconds: number, textStr: string) {
    //     this.maxCount = seconds;
    //     let countDownHandler = () => {
    //         this.maxCount--;
    //         // cc.log(this.maxCount);
    //         if (this.maxCount === 0) {
    //             this.getfixedMissage(textStr, seconds);
    //             this.unschedule(countDownHandler);
    //             return;
    //         }
    //     };
    //     this.schedule(countDownHandler, 1);
    // }

    /**
     * 开始滚动信息
     * @param content 滚动内容
     */
    public startScroll(data: { content: string, notifiId: number }): void {
        let self = this
        if (data.content == null || data.content.length == 0) {
            return
        }
        // this.contentArr.splice(0, 0, content)
        NotificationMgr.setNoticeScrolling(data);

        self.node.getChildByName("MainNode").active = true;

        if (self.label.node.getActionByTag(0) != null && this.label.node.getActionByTag(0).isDone() == false)//如果正在播放只插入数据
        {
            return
        }

        let scrollFunc = () => {
            if (NotificationMgr.getNoticeScrolling().length > 0) {
                let labeldata = NotificationMgr.getNoticeScrolling().shift();
                // NotificationMgr.getNoticeScrolling().shift()
                self.label.string = labeldata.content;

                if (NotificationMgr.getNoticeArr().length >= 15) {
                    NotificationMgr.getNoticeArr().splice(0, 1);
                }
                if (labeldata.notifiId) {
                    let lenArr = NotificationMgr.GetNoticeArrId();
                    if (lenArr.indexOf(labeldata.notifiId) == -1) {
                        NotificationMgr.setNoticeArr(self.label.string);
                        NotificationMgr.SetNoticeArrId(labeldata.notifiId);
                    }
                } else {
                    NotificationMgr.setNoticeArr(self.label.string);
                    // NotificationMgr.SetNoticeArrId(notifiId);
                }

                //需要先更新标签的宽度，不然下一帧才更新，这里取到的值就会是原来的值，导致宽度计算错误
                self.label._forceUpdateRenderData(true)
                self.label.node.position = self.startPos
                let distance: number = self.label.node.width + self.label.node.parent.width
                let duration: number = distance / 100
                let seq = cc.sequence(
                    cc.moveBy(duration, cc.v2(-distance, 0)),
                    cc.callFunc(function () {
                        self.label.string = ""
                        self.label.node.position = self.startPos
                        scrollFunc()
                    })
                )
                seq.setTag(0)
                self.label.node.runAction(seq)
                self.setInfoList();
            }
            else {
                self.ScrollMsgList.active = false;
                self.node.getChildByName("MainNode").active = false;
            }
        }
        scrollFunc()
    }

    public setInfoList() {
        let content = this.ScrollMsgList.getChildByName("ScrollView").getChildByName("view").getChildByName("content");
        content.removeAllChildren();

        for (const k of NotificationMgr.getNoticeArr()) {
            let itemNode = cc.instantiate(this.listItem);
            content.addChild(itemNode);
            itemNode.getComponent("lobby_scrollmsg_itemTs").setInfo(k);
        }
    }

    public onDestroy() {
        if (!this.label.node) return;
        if (this.label.node.getActionByTag(0) != null) {
            this.label.node.stopAction(this.label.node.getActionByTag(0))
        }
    }

    /**2014-06-18 10:33:24 */
    public timestampToTime(timestamp): string {
        var date = new Date(timestamp * 1000);//时间戳为10位需*1000，时间戳为13位的话不需乘1000
        var Y = date.getFullYear() + '-';
        var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
        var D = (date.getDate() < 10 ? '0' + date.getDate() : date.getDate()) + ' ';
        var h = (date.getHours() < 10 ? '0' + date.getHours() : date.getHours()) + ':';
        var m = (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()) + ':';
        var s = (date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds());
        return Y + M + D + h + m + s;
    }
}
